<template>
  <div class="sub-filter" v-if="filterData && !filterLoading">
    <div class="item">
      <div class="head">品牌：</div>
      <div class="body">
        <a
          @click="changeBarand(brand.id)"
          :class="{ active: filterData.selectedBrand === brand.id }"
          href="javaScript:;"
          v-for="brand in filterData.brands"
          :key="brand.id"
          >{{ brand.name }}</a
        >
      </div>
    </div>
    <div class="item" v-for="p in filterData.saleProperties" :key="p.id">
      <div class="head">{{ p.name }}：</div>
      <div class="body">
        <a
          @click="changeProp(p, attr.id)"
          :class="{ active: p.selectedProp == attr.id }"
          href="javaScript:;"
          v-for="attr in p.properties"
          :key="attr.id"
          >{{ attr.name }}</a
        >
      </div>
    </div>
  </div>
  <div v-else class="sub-filter">
    <XtxSkeleton animated class="item" width="800px" height="40px" />
    <XtxSkeleton animated class="item" width="800px" height="40px" />
    <XtxSkeleton animated class="item" width="600px" height="40px" />
    <XtxSkeleton animated class="item" width="600px" height="40px" />
    <XtxSkeleton animated class="item" width="600px" height="40px" />
  </div>
</template>
<script>
import { GetSub } from "@/api/index.js";
import { useRoute } from "vue-router";
import { ref, watch } from "vue";
export default {
  name: "SubFilter",
  setup(p, { emit }) {
    // 1. 获取数据
    // 2. 数据中需要全部选中，需要预览将来点击激活功能。默认选中全部
    // 3. 完成渲染
    const route = useRoute();
    const filterData = ref(null);
    const filterLoading = ref(false);
    // 4. 分类发生变化的时候需要重新获取筛选数据，需要使用侦听器
    watch(
      () => route.query.id,
      (newVal) => {
        // 当你从二级分类去顶级分类也会拿到ID，不能去加载数据因为它不是二级分类的ID
        filterLoading.value = true;
        newVal &&
          GetSub(route.query.id).then(({ result }) => {
            // 品牌全部
            result.selectedBrand = null;
            result.brands.unshift({ id: null, name: "全部" });
            // 销售属性全部
            result.saleProperties.forEach((p) => {
              p.selectedProp = undefined;
              p.properties.unshift({ id: null, name: "全部" });
            });
            filterData.value = result;
            filterLoading.value = false;
          });
      },
      { immediate: true }
    );
    // 品牌的id
    function changeBarand(id) {
      if (filterData.value.selectedBrand == id) return;
      filterData.value.selectedBrand = id;
      emit("changeBarand", getFilterParmas());
    }

    function changeProp(item, id) {
      if (item.selectedProp == id) return;
      // 把父级的item 传过来 再把子级的传过来 修改父级的 id
      item.selectedProp = id;
      emit("changeProp", getFilterParmas());
    }

    // 获取筛选数据的函数
    const getFilterParmas = () => {
      const obj = { brandId: null, attrs: [] };
      // 品牌属性
      obj.brandId = filterData.value.selectedBrand;
      // 销售属性
      filterData.value.saleProperties.forEach((item) => {
        // 循环父元素
        if (item.selectedProp) {
          // 有数据的时候找
          const prop = item.properties.find(
            // 找到父元素里面 子元素的
            (val) => val.id == item.selectedProp
          );
          obj.attrs.push({ groupName: item.name, propertyName: prop.name });
        }
      });
      if (obj.attrs.length === 0) obj.attrs = null;
      return obj;
    };

    console.log(filterLoading.value);
    return {
      filterData,
      filterLoading,
      changeBarand,
      changeProp,
      getFilterParmas,
    };
  },
};
</script>
<style scoped lang="scss">
.active {
  color: $xtxColor !important;
}
.xtx-skeleton {
  padding: 10px 0;
}
// 筛选区
.sub-filter {
  background: #fff;
  padding: 25px;
  .item {
    display: flex;
    line-height: 40px;
    .head {
      width: 80px;
      color: #999;
    }
    .body {
      flex: 1;
      a {
        margin-right: 36px;
        transition: all 0.3s;
        display: inline-block;
        &:hover {
          color: $xtxColor;
        }
      }
    }
  }
}
</style>
